/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package database;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author X.B.L
 */
public class PromoDAO {

private Connection conn = null;
  private Statement sqlStatement;
  private static final Logger logger = Logger.getLogger(PromoDAO.class.getName());

  /**
   *
   * @param conn
   * @param sqlStatement
   */
  public PromoDAO(Connection conn, Statement sqlStatement) {
    this.conn = conn;
    this.sqlStatement = sqlStatement;
  }

  public boolean addPromo(String promoName,
                          String startDate,
                          String endDate,
                          double promoAmount,
                          int storeCardReq) {

    String sqlText = "INSERT INTO `digistore`.`promotion` (`promotionName`, "
      + "`startDate`, `endDate`, `promoAmount`, `storeCardReq`) VALUES "
      + "('" + promoName + "', "
      + "'" + startDate + "', "
      + "'" + endDate + "', "
      + "'" + promoAmount + "', "
      + storeCardReq + ")";
    return updateDB(sqlText);
  }

  public boolean updatePromo(int prid,
                             String promoName,
                             String startDate,
                             String endDate,
                             double promoAmount,
                             int storeCardReq) {

    String sqlText = "UPDATE `digistore`.`promotion` SET "
      + "`promotionName`='" + promoName + "', "
      + "`startDate`='" + startDate + "', "
      + "`endDate`='" + endDate + "', "
      + "`promoAmount`='" + promoAmount + "', "
      + "`storeCardReq`=" + storeCardReq
      + " WHERE `prid`='" + prid + "'";
    return updateDB(sqlText);
  }

  public boolean addItemToPromotion(String upc, int prid) {
    String sqlText = "INSERT INTO `digistore`.`hasdiscount` (`upc`, `prid`) VALUES ("
      + "'" + upc + "', " + prid + ")";
    return updateDB(sqlText);
  }

  private boolean updateDB(String sqlText) {
    try {
      int value = sqlStatement.executeUpdate(sqlText);
      if (value == 1) {
          conn.commit();
          return true;
      }
    } catch (SQLException ex) {
      logger.log(Level.SEVERE, null, "ItemDAO : updateDB" + ex);
    }

    return false;
  }
}